﻿using System;
using System.ComponentModel;
using NGridDrawer.Partitions;

namespace NGridDrawer.Components
{
    /// <summary>
    /// Provide a base class for <see cref="IDrawerComponent"/> Interface
    /// </summary>
    public abstract partial class DrawerComponent : Component, IDrawerComponent
    {
        private IDrawerComponent _component;
        private IPartitionDrawer _drawer;


        /// <summary>
        /// 	<para>Initializes an instance of the <see cref="DrawerComponent"/> class.</para>
        /// </summary>
        protected DrawerComponent()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 	<para>Initializes an instance of the <see cref="DrawerComponent"/> class.</para>
        /// </summary>
        /// <param name="container">
        /// </param>
        protected DrawerComponent(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        #region IDrawerComponent Members

        /// <summary>Get Drawer property</summary>
        public virtual IPartitionDrawer Drawer
        {
            get { return _drawer; }
            protected set
            {
                if (_drawer == value) return;
                _drawer = value;
                OnPropertyChanged("Drawer");
            }
        }


        /// <summary>Get/Set Component Property</summary>
        public virtual IDrawerComponent Component
        {
            get { return _component; }
            set
            {
                if (_component == value) return;

                if (Equals(value))
                    throw new InvalidOperationException("Choose an other component please.");

                if (_component != null)
                    _component.PropertyChanged -= onPropertyChanged;

                _component = value;

                if (_component != null)
                {
                    _component.PropertyChanged += onPropertyChanged;
                }

                OnPropertyChanged("Component");
            }
        }


        ///<summary>Occurs when a property value changes.</summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion


        /// <summary>
        /// called when a property have been updated
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void onPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(sender, e);
        }

        /// <summary>
        /// <see cref="INotifyPropertyChanged"/> implementation
        /// </summary>
        /// <param name="propertyName">name of the property which has a changed value
        /// </param>
        protected void OnPropertyChanged(string propertyName)
        {
            onPropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }


    }
}